package com.xj0927.controller;

import com.xj0927.util.FileDfsUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author: xj0927
 * @Description:
 * @Date Created in 2020-11-14 15:36
 * @Modified By:
 */
@RestController
public class FileController {
    @Resource
    private FileDfsUtil fileDfsUtil;

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
    public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) {
        String result ;
        try {
            String path = fileDfsUtil.upload(file);
            if (StringUtils.isEmpty(path)) {
                result = "上传失败";
            } else {
                result = path;
            }
        } catch (IOException e) {
            e.printStackTrace();
            result = "服务器异常";
        }
        return ResponseEntity.ok(result);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResponseEntity<String> delete(String fileName) {
        fileDfsUtil.deleteFile(fileName);
        return ResponseEntity.ok("success");
    }
}
